package edu.sjb.threads;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

import edu.sjb.constants.SoundConstants;

public class SoundThread {

	protected static Clip bgmOverworld;
	protected static Clip sfxJump;
	protected static Clip bgmMarioDie;
	protected static Clip sfxGoombaCrush;
	protected static Clip sfxKoopaCrush;
	protected static Clip sfxPopPowerup;
	protected static Clip sfxFlower;
	protected static Clip sfxFireball;
	protected static Clip bgmStar;
	protected static Clip sfxMush1UP;
	protected static Clip sfxCoin;
	protected static Clip sfxNuclearBomb;
	protected static Clip bgmCleared;

	public SoundThread() {
		try {
			AudioInputStream bgmOverworldFile = AudioSystem
			        .getAudioInputStream(new File(SoundConstants.BGM_OVERWORLD));
			AudioFormat bgmOverworldFormat = bgmOverworldFile.getFormat();
			DataLine.Info bgmOverworldInfo = new DataLine.Info(Clip.class,
			        bgmOverworldFormat);
			bgmOverworld = (Clip) AudioSystem.getLine(bgmOverworldInfo);
			bgmOverworld.open(bgmOverworldFile);

			AudioInputStream sfxJumpFile = AudioSystem
			        .getAudioInputStream(new File(SoundConstants.SFX_JUMP));
			AudioFormat sfxJumpFormat = sfxJumpFile.getFormat();
			DataLine.Info sfxJumpInfo = new DataLine.Info(Clip.class,
			        sfxJumpFormat);
			sfxJump = (Clip) AudioSystem.getLine(sfxJumpInfo);
			sfxJump.open(sfxJumpFile);

			AudioInputStream bgmMarioDieFile = AudioSystem
			        .getAudioInputStream(new File(SoundConstants.BGM_DEATH));
			AudioFormat bgmMarioDieFormat = bgmMarioDieFile.getFormat();
			DataLine.Info bgmMarioDieInfo = new DataLine.Info(Clip.class,
			        bgmMarioDieFormat);
			bgmMarioDie = (Clip) AudioSystem.getLine(bgmMarioDieInfo);
			bgmMarioDie.open(bgmMarioDieFile);

			AudioInputStream sfxGoombaCrushFile = AudioSystem
			        .getAudioInputStream(new File(SoundConstants.SFX_GOOMBA));
			AudioFormat sfxGoombaCrushFormat = sfxGoombaCrushFile.getFormat();
			DataLine.Info sfxGoombaCrushInfo = new DataLine.Info(Clip.class,
			        sfxGoombaCrushFormat);
			sfxGoombaCrush = (Clip) AudioSystem.getLine(sfxGoombaCrushInfo);
			sfxGoombaCrush.open(sfxGoombaCrushFile);

			AudioInputStream sfxKoopaCrushFile = AudioSystem
			        .getAudioInputStream(new File(SoundConstants.SFX_KOOPA));
			AudioFormat sfxKoopaCrushFormat = sfxKoopaCrushFile.getFormat();
			DataLine.Info sfxKoopaCrushInfo = new DataLine.Info(Clip.class,
			        sfxKoopaCrushFormat);
			sfxKoopaCrush = (Clip) AudioSystem.getLine(sfxKoopaCrushInfo);
			sfxKoopaCrush.open(sfxKoopaCrushFile);

			AudioInputStream sfxFlowerFile = AudioSystem
			        .getAudioInputStream(new File(SoundConstants.SFX_FLOWER));
			AudioFormat sfxFlowerFormat = sfxFlowerFile.getFormat();
			DataLine.Info sfxFlowerInfo = new DataLine.Info(Clip.class,
			        sfxFlowerFormat);
			sfxFlower = (Clip) AudioSystem.getLine(sfxFlowerInfo);
			sfxFlower.open(sfxFlowerFile);

			AudioInputStream sfxFireballFile = AudioSystem
			        .getAudioInputStream(new File(SoundConstants.SFX_FIREBALL));
			AudioFormat sfxFireballFormat = sfxFireballFile.getFormat();
			DataLine.Info sfxFireballInfo = new DataLine.Info(Clip.class,
			        sfxFireballFormat);
			sfxFireball = (Clip) AudioSystem.getLine(sfxFireballInfo);
			sfxFireball.open(sfxFireballFile);

			AudioInputStream bgmStarFile = AudioSystem
			        .getAudioInputStream(new File(SoundConstants.BGM_STAR));
			AudioFormat bgmStarFormat = bgmStarFile.getFormat();
			DataLine.Info bgmStarInfo = new DataLine.Info(Clip.class,
			        bgmStarFormat);
			bgmStar = (Clip) AudioSystem.getLine(bgmStarInfo);
			bgmStar.open(bgmStarFile);

			AudioInputStream sfxMush1UPFile = AudioSystem
			        .getAudioInputStream(new File(SoundConstants.SFX_1UP));
			AudioFormat sfxMush1UPFormat = sfxMush1UPFile.getFormat();
			DataLine.Info sfxMush1UPInfo = new DataLine.Info(Clip.class,
			        sfxMush1UPFormat);
			sfxMush1UP = (Clip) AudioSystem.getLine(sfxMush1UPInfo);
			sfxMush1UP.open(sfxMush1UPFile);

			AudioInputStream sfxCoinFile = AudioSystem
			        .getAudioInputStream(new File(SoundConstants.SFX_COIN));
			AudioFormat sfxCoinFormat = sfxCoinFile.getFormat();
			DataLine.Info sfxCoinInfo = new DataLine.Info(Clip.class,
			        sfxCoinFormat);
			sfxCoin = (Clip) AudioSystem.getLine(sfxCoinInfo);
			sfxCoin.open(sfxCoinFile);

			AudioInputStream sfxNuclearBombFile = AudioSystem
			        .getAudioInputStream(new File(SoundConstants.SFX_NUC_BOMB));
			AudioFormat sfxNuclearBombFormat = sfxNuclearBombFile.getFormat();
			DataLine.Info sfxNuclearBombInfo = new DataLine.Info(Clip.class,
			        sfxNuclearBombFormat);
			sfxNuclearBomb = (Clip) AudioSystem.getLine(sfxNuclearBombInfo);
			sfxNuclearBomb.open(sfxNuclearBombFile);

			AudioInputStream sfxPopPowerUpFile = AudioSystem
			        .getAudioInputStream(new File(SoundConstants.SFX_BRICK_PWUP));
			AudioFormat sfxPopPowerUpFormat = sfxPopPowerUpFile.getFormat();
			DataLine.Info sfxPopPowerUpInfo = new DataLine.Info(Clip.class,
			        sfxPopPowerUpFormat);
			sfxPopPowerup = (Clip) AudioSystem.getLine(sfxPopPowerUpInfo);
			sfxPopPowerup.open(sfxPopPowerUpFile);

			AudioInputStream bgmClearedFile = AudioSystem
			        .getAudioInputStream(new File(SoundConstants.BGM_GOAL));
			AudioFormat bgmClearedFormat = bgmClearedFile.getFormat();
			DataLine.Info bgmClearedInfo = new DataLine.Info(Clip.class,
			        bgmClearedFormat);
			bgmCleared = (Clip) AudioSystem.getLine(bgmClearedInfo);
			bgmCleared.open(bgmClearedFile);
		}
		catch (UnsupportedAudioFileException e) {
		}
		catch (IOException e) {
		}
		catch (LineUnavailableException e) {
		}

	}

	public static void startBGMOverworld() {
		bgmOverworld.start();
		bgmOverworld.loop(Clip.LOOP_CONTINUOUSLY);
	}

	public static void jump() {
		sfxJump.loop(1);
	}

	public static void startBGMDead() {
		bgmStar.stop();
		bgmMarioDie.start();
	}

	public static void goombaCrush() {
		sfxGoombaCrush.start();
	}

	public static void koopaCrush() {
		sfxKoopaCrush.start();
	}

	public static void powerUp() {
		sfxPopPowerup.start();
	}

	public static void fireball() {
		sfxFlower.start();
	}

	public static void startBGMStar() {
		bgmStar.start();
		bgmStar.loop(Clip.LOOP_CONTINUOUSLY);
	}

	public static void mush1UP() {
		sfxMush1UP.start();
	}

	public static void coin() {
		sfxCoin.start();
	}

	public static void startSFXNuclearBomb() {
		sfxNuclearBomb.start();
	}

	public static void startBGMGoal() {
		bgmStar.stop();
		bgmCleared.start();
	}

	public static void stopBGMOverworld() {
		bgmOverworld.stop();
	}

	public static void stopBGMDead() {
		bgmMarioDie.stop();
	}

	public static void stopBGMStar() {
		bgmStar.stop();
	}

	public static void stopSFXNuclarBomb() {
		sfxNuclearBomb.stop();
	}
}
